//
//  AccountDetailsVC.m
//  NtoccShipper
//
//  Created by zcj on 2021/1/28.
//  Copyright © 2021 zhuchongjin. All rights reserved.
//

#import "AccountDetailsVC.h"
#import "AccountDetailModel.h"
#import "AccountDetailsCell.h"
#import "TiXianApplyVC.h"
#import "CJDateTime.h"
@interface AccountDetailsVC ()<UITableViewDelegate,UITableViewDataSource>{
    FlexFrameView  *header;
}
@property (nonatomic,strong) AccountDetailsCell *accountDetailsCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;

@property (nonatomic,strong) CJLabel *labStartTime;
@property (nonatomic,strong) CJLabel *labEndTime;
@property (nonatomic,copy) NSString *strStartTime;
@property (nonatomic,copy) NSString *strEndTime;
@property (nonatomic,copy) NSString *strTimeType; // 0 开始时间  1结束时间
@property (nonatomic,assign) BOOL isFirstLoad; // 首次加载

@end

@implementation AccountDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleName:@"账户明细"];
    [self setBackLeftButton:@""];
    [self setRightImage:CJLString(@"置空") image:@""];
    
    
    [self createTopView];

    self.accountDetailsCell = [[AccountDetailsCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    
    self.strStartTime = @"";
    self.strEndTime = @"";

    [self initData];
    [self setDefalutValue];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [self loadData];
    self.isFirstLoad = YES;
    [self.tableView.mj_header beginRefreshing];

}
- (void)RightTextClick:(UIButton *)sender{
   
    [self setDefalutValue];
    self.isFirstLoad = YES;
    [self.tableView.mj_header beginRefreshing];
}

- (void)setDefalutValue{
    self.strStartTime = @"";
    self.strEndTime = @"";
    //    self.strSelectType = @"";
    self.labStartTime.text = @"开始时间";
    self.labEndTime.text = @"结束时间";
}
- (void)createTopView{
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"AccountDetailHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;

    [header layoutIfNeeded];
    [self.view addSubview:header];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

- (void)loadData{
    
    if (!self.isFirstLoad) {
        if ([self.strStartTime isEqualToString:@""]) {
            [CJAlert showStringDismissWithDelay:@"请选择开始时间"];
            [self.tableView.mj_header endRefreshing];
            return;
        }
        
        if ([self.strEndTime isEqualToString:@""]) {
            [CJAlert showStringDismissWithDelay:@"请选择结束时间"];
            [self.tableView.mj_header endRefreshing];
            return;
        }
    }
    
    self.isFirstLoad = NO;
    
    // 比较时间
    
    if (![self.strEndTime isEqualToString:@""] && ![self.strStartTime isEqualToString:@""]) {
        NSDateComponents *dateComp = [CJDateTime components:NSCalendarUnitDay timesType:CJDateTimeModeYMD fromStrDate:self.strStartTime toStrDate:self.strEndTime];

        if (dateComp.day < 0) {
            [CJAlert showStringDismissWithDelay:@"开始时间必须小于结束时间"];
            [self.tableView.mj_header endRefreshing];
            return;
        }

        if (dateComp.day >= 90) {
            
            [CJAlert showStringDismissWithDelay:@"查询时间间隔不能超过90天"];
            
            if (self.strTimeType.intValue == 0) {
                self.strStartTime = @"";
                self.labStartTime.text = @"开始时间";
            }else{
                self.strEndTime = @"";
                self.labEndTime.text = @"结束时间";
            }
            
            [self.tableView.mj_header endRefreshing];
            return;
        }
    }
    
    
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/mybankVostroChargeBatchQuery",CJBaseUrl);
    
    NSDictionary *dicPra = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"startTime":[self.strStartTime stringByReplacingOccurrencesOfString:@"-" withString:@""],
                             @"endTime":[self.strEndTime stringByReplacingOccurrencesOfString:@"-" withString:@""]
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
//        if (list.count > 0) {

           if ([ws.refreshType isEqualToString:@"down"]) {
               [ws.modelArray removeAllObjects];
           }

           NSError * error ;
           NSArray *arr = [AccountDetailModel arrayOfModelsFromDictionaries:list error:&error];
           [ws.modelArray addObjectsFromArray:arr];

           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshing];

           if ( list.count < 10) {
               [ws.tableView.mj_footer endRefreshingWithNoMoreData];
           }

//        }else{
//           [ws.modelArray removeAllObjects];
//           [ws.tableView.mj_header endRefreshing];
//           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        [ws.tableView reloadData];
       

    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];

}


- (void)downRefresh
{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh
{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    static NSString *identifier = @"AccountDetailsCell";
    AccountDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[AccountDetailsCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    AccountDetailModel *model = self.modelArray[indexPath.row];
    [cell sewtCellInfoWithModel:model];
    
    WS(ws);
    cell.blockTiXian = ^(id anyObject) {
        
        TiXianApplyVC *vc = [[TiXianApplyVC alloc]init];
        vc.maxTiXianMoney = model.maxRefundAmount;
        vc.relateOrderNo = model.orderNo;
        [ws pushVc:vc];
        
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}

#pragma mark ----------btnActionAction

- (void)btnSelectStartTimeAction{
   
    [self btnShowDataTimeWithType:@"0"];
}
- (void)btnSelectEndimeAction{
    [self btnShowDataTimeWithType:@"1"];
}

- (void)btnShowDataTimeWithType:(NSString *)strType{
    
    WS(ws);
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = strType.intValue == 0 ? @"开始时间":@"截止时间";
    datePickerView.minDate = [NSDate br_setYear:2016 month:5 day:1];
    datePickerView.maxDate = [NSDate date];  //[NSDate br_setYear:2050 month:5 day:1];
    datePickerView.resultBlock = ^(NSString *selectValue) {

        ws.strTimeType = strType;
        if (strType.intValue == 0) {
            ws.labStartTime.text = selectValue;
            ws.strStartTime = CJStringWithFormat(@"%@",selectValue);
        } else {
            ws.labEndTime.text = selectValue;
            ws.strEndTime = CJStringWithFormat(@"%@",selectValue);
        }
        [ws.modelArray removeAllObjects];
//        [ws.tableView.mj_header beginRefreshing];
        [ws downRefresh];
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    datePickerView.pickerStyle = customStyle;
    [datePickerView show];

}



#pragma mark ----------lazy
- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, header.bottom , SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight - header.height),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
            
        };

    }
    
    return _tableView;
}


@end


